package top.lixb.libcommon.base;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

public abstract class SimpleBindingAdapter<T,B extends ViewDataBinding> extends RecyclerView.Adapter<ItemHolder<B>> {

    private int mItemLayout;
    public List<T> mData;

    public SimpleBindingAdapter(@LayoutRes int itemLayout) {
        mItemLayout = itemLayout;
    }

    @NonNull
    @Override
    public ItemHolder<B> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        B binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),mItemLayout,parent,false);
        ItemHolder<B> itemHolder = new ItemHolder<>(binding.getRoot());
        itemHolder.setBinding(binding);
        return itemHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder<B> holder, int position) {
        bind(holder,mData.get(position));
    }

    public abstract void bind(@NonNull ItemHolder<B> holder, T bean);

    @Override
    public int getItemCount() {
        return mData==null?0:mData.size();
    }

    public void setNewData(List<T> newData) {
        mData = newData;
        notifyDataSetChanged();
    }

    public void addData(List<T> data) {
        if (null != mData && null!=data) {
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }


}
